package com.zhugang.week13.tree;

/**
 * @program algorithms
 * @description: invertTree
 * @author: chanzhugang
 * @create: 2022/12/30 15:10
 */
public class InvertTree2 {

    /**
     * 226 翻转二叉树
     * https://leetcode.cn/problems/invert-binary-tree/
     *
     * @param root
     * @return
     */
    public TreeNode invertTree(TreeNode root) {
        /**
         * brew 作者没写出来这题。
         * 左右子树翻转
         */
        if (root == null) {
            return root;
        }
        TreeNode left = invertTree(root.left);
        TreeNode right = invertTree(root.right);
        root.left = right;
        root.right = left;
        return root;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}